import { reactive } from "vue";
import router from "../router/index";
import axios from "axios";
import { Toast } from "vant";
/**
 * 注册请求接口
 */
export const RequestReq = () => {
  const user = reactive({
    username: "",
    password: "",
    age: "",
    nickname: "",
    fullname: "",
    sex: "",
  });
  const backStart = () => {
    history.back();
  };
  const onRegister = async () => {
    const { username, password, nickname, age, sex, fullname } = user;
    let res = await axios({
      method: "post",
      url: "/api/users/UserRegister",
      data: {
        username,
        password,
        nickname,
        age,
        fullname,
        sex,
      },
    });
    //   console.log(res);

    if (res.data.Code == 1) {
      Toast({
        message: "注册成功",
        position: "top",
      });

      router.push({ path: "/login" });
    } else {
      Toast({
        message: "注册失败",
        position: "top",
      });
      console.log("注册失败");
      return false;
    }
  };
  return {
    user,
    onRegister,
    backStart,
  };
};

/**
 * 登录请求接口
 */
export const LoginReq = () => {
  const user = reactive({
    username: "",
    password: "",
  });

  const backStart = () => {
    history.back();
  };

  const onLogin = () => {
    const { username, password } = user;
    axios({
      method: "post",
      url: "/api/users/UserLogin",
      data: {
        username,
        password,
      },
    })
      .then((data) => {
        console.log(data.data.info);
        if (data.data.Code == 1) {
          Toast({
            message: "登录成功",
            position: "top",
          });
            // getBanner()
          sessionStorage.setItem("user_data", JSON.stringify(data));
          router.push({ path: "/home/index" });
        } else {
            Toast({
                message: '输入错误，登录失败！',
                position:'top',
            })
        }
      })
      .catch((error) => {
        console.log(error);
      });
  };

  return { user, onLogin, backStart };
};

// 修改就诊人信息
export const ChangeReq = () => {
    const user = reactive({
        username: "",
        password: "",
        nickname: "",
        fullname: "",
        age: "",
        sex: "",
    });

    console.log(1);

    let userdata = JSON.parse(sessionStorage.getItem('user_data'))
    
    const ChangeUser = (user) => {

    let ids = userdata.data.info._id
    

         let {username,
                password,
                fullname,
                nickname,
                age,
            sex } = user
        
        axios({
            method: 'post',
            url: `/api/admin/UpdateUser`,
            data: {
                _id:ids,
                username,
                password,
                fullname,
                nickname,
                age,
                sex,
            }
        }).then((data) => {
            console.log(data);
            
            if (data.data.Code == 1) {
                Toast({
                    message: '修改成功,请重新登录！',
                    position:'top',
                })
                router.push({path:'/login'})
            } else {
                Toast({
                    message: '修改失败！',
                    position:'top',
                })
            }
        }).catch((error) => {
            console.log(error);
            
        })
    }

    return {ChangeUser,user}
}
// 获取banner数据
const getBanner = async() => {
    let res = await axios({
        method: 'get',
        url: '/api/admin/AllqueryBanner',
    })
    // console.log(res);
    localStorage.setItem('bannerList',JSON.stringify(res.data.data))
}


// 查询用户预约接口
export const QueryReqAppointment = () => {
    let userdata = JSON.parse(sessionStorage.getItem('user_data'))
    let ids = userdata.data.info._id
    // console.log(userdata);
    console.log('id>>>>>>>>', ids);
    
  const QueryUserAppointment = async (ids:string) => {
    // const { username, password, nickname, age, fullname, sex } = user;
    let res_user = await axios({
      method: "get",
        url: "/api/admin/queryUser/",
        data: {
            _id: ids,
      }
    //   data: {
    //     username,
    //     password,
    //     nickname,
    //     age,
    //     fullname,
    //     sex,
    //   },
    });
      console.log('你好');
      
      if (res_user.data.Code == 1) {
          console.log('config>>>>信息', res_user.config);   
      }else {
          Toast({
              message: '您还未预约！',
              position:'top',
          })
         router.push({path:'/home/order'})
      }
    console.log('id获取用户信息--',res_user);
};
  return {
    QueryUserAppointment,
    // user,
  };
};
